package com.vibration.baseinfo.service.impl;

import java.text.SimpleDateFormat;
import java.util.*;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.vibration.common.core.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.vibration.baseinfo.mapper.CzMoreTemperatureDataMapper;
import com.vibration.baseinfo.domain.CzMoreTemperatureData;
import com.vibration.baseinfo.service.ICzMoreTemperatureDataService;

/**
 * 多点位温度Service业务层处理
 * 
 * @author Mr_Wu
 * @date 2023-01-31
 */
@Service
public class CzMoreTemperatureDataServiceImpl implements ICzMoreTemperatureDataService 
{
    @Autowired
    private CzMoreTemperatureDataMapper czMoreTemperatureDataMapper;

    /**
     * 查询多点位温度
     * 
     * @param id 多点位温度主键
     * @return 多点位温度
     */
    @Override
    public CzMoreTemperatureData selectCzMoreTemperatureDataById(Long id)
    {
        return czMoreTemperatureDataMapper.selectCzMoreTemperatureDataById(id);
    }

    /**
     * 查询多点位温度列表
     * 
     * @param czMoreTemperatureData 多点位温度
     * @return 多点位温度
     */
    @Override
    public List<CzMoreTemperatureData> selectCzMoreTemperatureDataList(CzMoreTemperatureData czMoreTemperatureData)
    {
        return czMoreTemperatureDataMapper.selectCzMoreTemperatureDataList(czMoreTemperatureData);
    }

    /**
     * 新增多点位温度
     * 
     * @param czMoreTemperatureData 多点位温度
     * @return 结果
     */
    @Override
    public int insertCzMoreTemperatureData(CzMoreTemperatureData czMoreTemperatureData)
    {
        return czMoreTemperatureDataMapper.insertCzMoreTemperatureData(czMoreTemperatureData);
    }

    /**
     * 修改多点位温度
     * 
     * @param czMoreTemperatureData 多点位温度
     * @return 结果
     */
    @Override
    public int updateCzMoreTemperatureData(CzMoreTemperatureData czMoreTemperatureData)
    {
        return czMoreTemperatureDataMapper.updateCzMoreTemperatureData(czMoreTemperatureData);
    }

    /**
     * 批量删除多点位温度
     * 
     * @param ids 需要删除的多点位温度主键
     * @return 结果
     */
    @Override
    public int deleteCzMoreTemperatureDataByIds(Long[] ids)
    {
        return czMoreTemperatureDataMapper.deleteCzMoreTemperatureDataByIds(ids);
    }

    /**
     * 删除多点位温度信息
     * 
     * @param id 多点位温度主键
     * @return 结果
     */
    @Override
    public int deleteCzMoreTemperatureDataById(Long id)
    {
        return czMoreTemperatureDataMapper.deleteCzMoreTemperatureDataById(id);
    }

    @Override
    public AjaxResult getTemperatureChartData(CzMoreTemperatureData czMoreTemperatureData) {
        List<CzMoreTemperatureData> list = czMoreTemperatureDataMapper.selectCzMoreTemperatureDataList(czMoreTemperatureData);
        List<Double> temp5 = new ArrayList<>();
        List<Double> temp10 = new ArrayList<>();
        List<Double> temp15 = new ArrayList<>();
        List<Double> temp20 = new ArrayList<>();
        List<Double> temp25 = new ArrayList<>();
        List<Double> temp30 = new ArrayList<>();
        List<Double> temp35 = new ArrayList<>();
        List<Double> temp40 = new ArrayList<>();
        List<Double> temp45 = new ArrayList<>();
        List<Double> temp50 = new ArrayList<>();
        List<String> timeList = new ArrayList<>();
        SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
        for (CzMoreTemperatureData moreTemperatureData : list) {
            temp5.add(moreTemperatureData.getTemperature5());
            temp10.add(moreTemperatureData.getTemperature10());
            temp15.add(moreTemperatureData.getTemperature15());
            temp20.add(moreTemperatureData.getTemperature20());
            temp25.add(moreTemperatureData.getTemperature25());
            temp30.add(moreTemperatureData.getTemperature30());
            temp35.add(moreTemperatureData.getTemperature35());
            temp40.add(moreTemperatureData.getTemperature40());
            temp45.add(moreTemperatureData.getTemperature45());
            temp50.add(moreTemperatureData.getTemperature50());
            timeList.add(format.format(moreTemperatureData.getDataTime()));
        }
        Map<String,List<Double>> chartMap = new HashMap<>();
        chartMap.put("5m",temp5);
        chartMap.put("10m",temp10);
        chartMap.put("15m",temp15);
        chartMap.put("20m",temp20);
        chartMap.put("25m",temp25);
        chartMap.put("30m",temp30);
        chartMap.put("35m",temp35);
        chartMap.put("40m",temp40);
        chartMap.put("45m",temp45);
        chartMap.put("50m",temp50);
        AjaxResult result = AjaxResult.success();
        result.put("total",list.size());
        JSONObject object = new JSONObject();
        object.put("timeList",timeList);
        object.put("temperatureData",chartMap);
        result.put("chartData",object);
        return result;
    }

    @Override
    public AjaxResult getTemperatureChartDataIndex(String equipmentCode,Integer type) {

        List<CzMoreTemperatureData> list = new ArrayList<>();
        if(type.intValue() == 1){ //默认
            list = czMoreTemperatureDataMapper.selectCzMoreTemperatureDataListLast20(equipmentCode);
        }else {
            Calendar c = Calendar.getInstance();
            for (int i = 0; i < 20; i++) {
                CzMoreTemperatureData data = czMoreTemperatureDataMapper.selectTemperatureDataLast(equipmentCode,c.getTime());
                if(data!=null){
                    list.add(data);
                }
                if(type.intValue() == 2){ //一小时
                    c.add(Calendar.HOUR_OF_DAY,-1);
                }else if(type.intValue() == 3){ //12小时
                    c.add(Calendar.HOUR_OF_DAY,-12);
                }else if(type.intValue() == 4){//1天
                    c.add(Calendar.DAY_OF_MONTH,-1);
                }else if(type.intValue() == 5){//1周
                    c.add(Calendar.WEEK_OF_MONTH,-1);
                }else if(type.intValue() == 6){//1月
                    c.add(Calendar.MONTH,-1);
                }else if(type.intValue() == 7){ //一年
                    c.add(Calendar.YEAR,-1);
                }
            }
        }
        //散点图数据
        List<List<Object>> scatterList = new ArrayList<>();

        List<Double> temp5 = new ArrayList<>();
        List<Double> temp10 = new ArrayList<>();
        List<Double> temp15 = new ArrayList<>();
        List<Double> temp20 = new ArrayList<>();
        List<Double> temp25 = new ArrayList<>();
        List<Double> temp30 = new ArrayList<>();
        List<Double> temp35 = new ArrayList<>();
        List<Double> temp40 = new ArrayList<>();
        List<Double> temp45 = new ArrayList<>();
        List<Double> temp50 = new ArrayList<>();
        List<String> timeList = new ArrayList<>();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (int i = list.size()-1; i >= 0; i--) {
            temp5.add(list.get(i).getTemperature5());
            temp10.add(list.get(i).getTemperature10());
            temp15.add(list.get(i).getTemperature15());
            temp20.add(list.get(i).getTemperature20());
            temp25.add(list.get(i).getTemperature25());
            temp30.add(list.get(i).getTemperature30());
            temp35.add(list.get(i).getTemperature35());
            temp40.add(list.get(i).getTemperature40());
            temp45.add(list.get(i).getTemperature45());
            temp50.add(list.get(i).getTemperature50());
            timeList.add(format.format(list.get(i).getDataTime()));

            List<Object> temList = new ArrayList<>();
            temList.add(format.format(list.get(i).getDataTime()));
            temList.add(list.get(i).getTemperature5());
            temList.add(list.get(i).getTemperature10());
            temList.add(list.get(i).getTemperature15());
            temList.add(list.get(i).getTemperature20());
            temList.add(list.get(i).getTemperature25());
            temList.add(list.get(i).getTemperature30());
            temList.add(list.get(i).getTemperature35());
            temList.add(list.get(i).getTemperature40());
            temList.add(list.get(i).getTemperature45());
            temList.add(list.get(i).getTemperature50());
            scatterList.add(temList);
        }
        Map<String,List<Double>> chartMap = new HashMap<>();
        chartMap.put("5m",temp5);
        chartMap.put("10m",temp10);
        chartMap.put("15m",temp15);
        chartMap.put("20m",temp20);
        chartMap.put("25m",temp25);
        chartMap.put("30m",temp30);
        chartMap.put("35m",temp35);
        chartMap.put("40m",temp40);
        chartMap.put("45m",temp45);
        chartMap.put("50m",temp50);
        AjaxResult result = AjaxResult.success();
        result.put("total",list.size());
        JSONObject object = new JSONObject();
        object.put("timeList",timeList);
        object.put("temperatureData",chartMap);
        object.put("scatterData",scatterList);
        result.put("chartData",object);
        return result;
    }


    @Override
    public CzMoreTemperatureData getLastOne(String gyroscopeCode) {
        return czMoreTemperatureDataMapper.getLastOne(gyroscopeCode);
    }

    /**
     * 计算查询温度集合
     */
    @Override
    public List<CzMoreTemperatureData> calculation(CzMoreTemperatureData czMoreTemperatureData) {
        List<CzMoreTemperatureData> tempList = selectCzMoreTemperatureDataList(czMoreTemperatureData);
        return tempList;
    }
}
